<?php
/***
	客户及潜在客户表，包括：
	1. 查询所有客户；
	2. 各种条件查询；
	3. 删除客户记录；
	4. 修改客户记录。
**/
class Client_model extends CI_Model {
	
	public function __construct() {
		$this->load->database();
	}
	//所有
	public function get_all() {
		
		$sqlstring = "select clients.*, a.contact_date as cd1, b.contact_date as cd2, a.status as status1, b.status as status2 from clients 
			left join contact_record as a on clients.record1 = a.id 
			left join contact_record as b on clients.record2 = b.id";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
		
	}
	
	public function get_all_by_sale() {
		$user_name = $_SESSION['user_name'];
		$sqlstring = "select clients.*, a.contact_date as cd1, b.contact_date as cd2, a.status as status1, b.status as status2 from clients 
			left join contact_record as a on clients.record1 = a.id 
			left join contact_record as b on clients.record2 = b.id
			where contact_by ='".$user_name."'";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
		
	}
	
	//只返回所有name
	public function get_name_all() {
		
		$sqlstring = "select name from clients";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
		
	}
	
	
	//列出未校核客户
	public function get_uncheck() {
		
		$query = $this->db->get_where('clients', array('check' => 'NO'));
		return $query->result_array();
	}
	//列出潜在客户
	public function get_type() {
		
		$type = "potantial";
		$query = $this->db->get_where('clients', array('type' => $type));
		return $query->row_array();
	}
	//按行业类别搜索
	public function get_sector($sector = FALSE) {
		
		$query = $this->db->get_where('clients', array('sector' => $sector));
		return $query->row_array();
	}
	
	//按id查询
	public function get_by_id($id = FALSE) {
		
		$query = $this->db->get_where('clients', array('id' => $id));
		return $query->row_array();
	}
	
	//新增客户信息
	public function create () {
		
		$client_name = $this->input->post('name');
		
		$data = array(
			'name' => $client_name,
			'sector' => $this->input->post('sector'),
			'type' => $this->input->post('type'),
			'remark' => $this->input->post('remark'),
			'website' => $this->input->post('website'),
			'country' => $this->input->post('country'),
			'contact_by' => $this->input->post('contact_by'),
			'scope' => $this->input->post('scope'),
			'address' => $this->input->post('address'),
			'check' => 'NO'
			
		);
		$this->db->insert('clients', $data);
		//获取新增记录的id
		$id = $this->db->insert_id();
		$data['client_id'] = $id;
		$data['client_name'] = $client_name;
		
		//新增一名叫做Others的联系人
		$contacter = array(
			'name' => 'Others',
			'client_id' => $id
		);
		
		$this->db->insert('contact_person', $contacter);
		return $data;
	}
	
	//通过校核
	public function update_check ($id) {
		$data = array(
			'check' => 'YES'
		);
		
		$this->db->where('id', $id);
		$this->db->update('clients', $data);
	}
	
	//修改客户记录
	public function update_all ($id) {
		$data = array(
			'name' => $this->input->post('name'),
			'sector' => $this->input->post('sector'),
			'type' => $this->input->post('type'),
			'remark' => $this->input->post('remark'),
			'website' => $this->input->post('website'),
			'country' => $this->input->post('country'),
			'contact_by' => $this->input->post('contact_by'),
			'address' => $this->input->post('address'),
			'scope' => $this->input->post('scope')			
		);
		
		$this->db->where('id', $id);
		$this->db->update('clients', $data);
	}
	

}